package models

import (
	"gorm.io/gorm"
	"time"
	"user_srv/basic/config"
)

// 个人用户充值记录表
type PersonalUserRecharge struct {
	Id                      uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	UserId                  int32          `gorm:"column:user_id;type:int;default:NULL;" json:"user_id"`
	RechargeTransactionId   string         `gorm:"column:recharge_transaction_id;type:varchar(255);comment:充值流水号;default:NULL;" json:"recharge_transaction_id"`
	UserName                string         `gorm:"column:user_name;type:varchar(255);comment:用户名;default:NULL;" json:"user_name"`
	Mobile                  string         `gorm:"column:mobile;type:varchar(255);comment:手机号码;default:NULL;" json:"mobile"`
	UserType                string         `gorm:"column:user_type;type:varchar(255);comment:用户类型;default:NULL;" json:"user_type"`
	RechargeTime            time.Time      `gorm:"column:recharge_time;type:datetime;comment:充值时间;default:NULL;" json:"recharge_time"`
	RechargeAmountPrincipal string         `gorm:"column:recharge_amount_principal;type:varchar(255);comment:充值金额-本金（元）;default:NULL;" json:"recharge_amount_principal"`
	GiftAmount              string         `gorm:"column:gift_amount;type:varchar(255);comment:赠送金额（元）;default:NULL;" json:"gift_amount"`
	RechargeDiscount        string         `gorm:"column:recharge_discount;type:varchar(255);comment:充值折扣;default:NULL;" json:"recharge_discount"`
	ThirdPartyTransactionId string         `gorm:"column:third_party_transaction_id;type:varchar(255);comment:第三方流水号;default:NULL;" json:"third_party_transaction_id"`
	RechargeChannel         string         `gorm:"column:recharge_channel;type:varchar(255);comment:充值渠道;default:NULL;" json:"recharge_channel"`
	CreatedAt               time.Time      `gorm:"column:created_at;type:datetime(3);default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt               time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt               gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

// 创建个人用户充值记录
func (p *PersonalUserRecharge) Create() error {
	return config.DB.Create(p).Error
}

// 根据ID获取个人用户充值记录
func (p *PersonalUserRecharge) GetById() error {
	return config.DB.Where("id = ?", p.Id).First(p).Error
}

// 根据用户ID获取个人用户充值记录列表
func (p *PersonalUserRecharge) GetByUserId() ([]*PersonalUserRecharge, error) {
	var list []*PersonalUserRecharge
	err := config.DB.Where("user_id = ?", p.UserId).Find(&list).Error
	return list, err
}

// 根据充值流水号获取记录
func (p *PersonalUserRecharge) GetByTransactionId() error {
	return config.DB.Where("recharge_transaction_id = ?", p.RechargeTransactionId).First(p).Error
}

// 分页查询个人用户充值记录
func (p *PersonalUserRecharge) GetList(transactionId, mobile, userName, channel, startTime, endTime string, page, pageSize int) ([]*PersonalUserRecharge, int64, error) {
	var list []*PersonalUserRecharge
	var total int64

	query := config.DB.Model(p)

	// 添加查询条件
	if transactionId != "" {
		query = query.Where("recharge_transaction_id LIKE ?", "%"+transactionId+"%")
	}
	if mobile != "" {
		query = query.Where("mobile LIKE ?", "%"+mobile+"%")
	}
	if userName != "" {
		query = query.Where("user_name LIKE ?", "%"+userName+"%")
	}
	if channel != "" && channel != "全部渠道" {
		query = query.Where("recharge_channel = ?", channel)
	}
	if startTime != "" {
		query = query.Where("recharge_time >= ?", startTime)
	}
	if endTime != "" {
		query = query.Where("recharge_time <= ?", endTime)
	}

	// 获取总数
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 分页查询
	offset := (page - 1) * pageSize
	err := query.Order("recharge_time DESC").Offset(offset).Limit(pageSize).Find(&list).Error

	return list, total, err
}
